package cn.com.ccxe.dao.impl;

import java.util.List;

import cn.com.ccxe.core.entity.Website;
import cn.com.ccxe.dao.DBHandler;
import cn.com.ccxe.dao.InsertException;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.dao.WebsiteDao;
import cn.com.ccxe.dao.parser.WebsiteParser;

public class WebsiteDaoImpl extends DBHandler implements WebsiteDao {

	private static final String SELECT_EXPR = 
		"SELECT  T.ID moduleId, T.PARENT_ID parentId, " +
			" T.DISCRIPTION description,T.NAME moduleName, " +
			" T.PATH modulePath, T.IS_LIST isList, T.IS_MONITOR isMonitor "+
			" FROM C_MODULE T ";
	
	private static WebsiteParser parser = new WebsiteParser();

	public OperationStatus add(Website module) {
		String sql = 
				" INSERT INTO C_MODULE " +
					" (ID, NAME, PARENT_ID, DISCRIPTION, PATH, IS_LIST, IS_MONITOR) "+
					" VALUES (SEQ_C_MODULE.NEXTVAL,?,?,?,?,?,?) ";
		Object[] parameters = new Object[] {
			module.getName(),
			module.getParentId(),
			module.getDiscription(),
			module.getPath(),
			module.getIsList(),
			module.getIsMonitor()
		};
		return insertOrUpdateOrDelete(sql, parameters);
	}
	
	public int addAndRetuenId(Website module) {
		int id = queryInt("SELECT SEQ_C_MODULE.NEXTVAL FROM DUAL",null);
		String sql = 
				" INSERT INTO C_MODULE " +
					" (ID, NAME, PARENT_ID, DISCRIPTION, PATH, IS_LIST, IS_MONITOR) "+
					" VALUES (?,?,?,?,?,?,?) ";
		Object[] parameters = new Object[] {
			id,
			module.getName(),
			module.getParentId(),
			module.getDiscription(),
			module.getPath(),
			module.getIsList(),
			module.getIsMonitor()
		};
		OperationStatus status = insertOrUpdateOrDelete(sql, parameters);
		if(status == OperationStatus.SUCCESS) {
			return id;
		} else {
			throw new InsertException("添加失败！");
		}
	}

	public OperationStatus del(int id) {
		String sql = 
				"DELETE C_MODULE T WHERE T.ID=?";
		return insertOrUpdateOrDelete(sql, new Object[]{id});
	}
	
	public OperationStatus del (String url) {
		String sql = 
				"DELETE C_MODULE T WHERE T.PATH = ?";
		return insertOrUpdateOrDelete(sql, new Object[]{url});
	}

	public Website get(int id) {
		StringBuilder sql = new StringBuilder(SELECT_EXPR);
		sql.append(" WHERE ID = ?");
		return queryObject(sql.toString(), new Object[]{id}, parser);
	}

	public List<Website> getListPageByParentId(int parentId) {
		StringBuilder sql = new StringBuilder(SELECT_EXPR);
		sql.append(" WHERE T.PARENT_ID = ? AND T.IS_LIST=1");
		return queryList(sql.toString(), new Object[]{parentId}, parser);
	}

	public List<Website> getChildWebsite(int parentId) {
			StringBuilder sql = new StringBuilder(SELECT_EXPR);
			sql.append(" WHERE T.PARENT_ID = ?");
		return queryList(sql.toString(), new Object[]{parentId}, parser);
	}

	public List<Website> getTopWebsite() {
		StringBuilder sql = new StringBuilder(SELECT_EXPR);
		sql.append(" WHERE T.PARENT_ID = 0");
		return queryList(sql.toString(), new Object[]{}, parser);
	}

	public OperationStatus update(Website module) {
		String sql = 
				" update c_module T " +
				" set T.NAME=?,T.DISCRIPTION=?,T.PATH=?, " +
				" 	  T.IS_LIST=?,T.IS_MONITOR=?" +
				" where T.ID = ?";
		Object[] parameters = new Object[] {
				module.getName(),
				module.getDiscription(),
				module.getPath(),
				module.getIsList(),
				module.getIsMonitor(),
				module.getId()
			};
		return insertOrUpdateOrDelete(sql, parameters);
	}

	public int countChild(int moduleId) {
		String sql = 
				" SELECT COUNT(0) " +
				" FROM C_MODULE T " +
				" WHERE T.PARENT_ID = ?";
		return queryInt(sql, new Object[]{moduleId});
	}

	public List<Website> getAllListPage() {
		StringBuilder sql = new StringBuilder(SELECT_EXPR);
		sql.append(" WHERE T.IS_LIST=1");
		return queryList(sql.toString(), new Object[]{}, parser);
	}

	public int getParentWebsiteId(int id) {
		String sql = 
			"SELECT PARENT_ID FROM C_MODULE WHERE ID = ?";
		return queryInt(sql, new Object[]{id});
	}

	public static void main(String[] args) {
		System.out.println(SELECT_EXPR);
	}

	public int countSubWebsite(int id) {
		String sql = "SELECT COUNT(0) FROM C_MODULE WHERE PARENT_ID = ?";
		return queryInt(sql, new Object[]{id});
	}

}
